package com.itheima.d6_threadhomework0910.num1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 操作类
 */
public class MoneySystem {
    private Account account;
    private final Lock lock = new ReentrantLock();


    public MoneySystem(Account account) {
        this.account = account;
    }
    //创建一个方法 实现存钱
    public void saveMoney(double money){
        //得到该线程的对象
        String name = Thread.currentThread().getName();
        lock.lock();
        try{
            account.setMoney(account.getMoney()+money);
            System.out.println(name+"存入"+money+"元后，账户的余额是:"+ account.getMoney());
        }catch( Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
}
